use std::{fmt::Debug, sync::Arc};

use tokio::sync::{
    mpsc::{channel, Receiver, Sender},
    Mutex,
};

#[derive(Debug)]
pub struct C<T>
where
    T: Debug,
{
    pub tx: Sender<T>,
    pub rx: Arc<Mutex<Receiver<T>>>,
}

impl<T> C<T>
where
    T: Debug,
{
    pub fn new(buffer: usize) -> Self {
        let (tx, rx) = channel(buffer);
        Self {
            tx,
            rx: Arc::new(Mutex::new(rx)),
        }
    }

    pub async fn send(&self, msg: T) {
        self.tx.send(msg).await.unwrap();
    }

    pub async fn recv(&mut self) -> Option<T> {
        self.rx.clone().lock().await.recv().await
    }
}
